package com.example.labb2da523b;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collections;

import android.app.ActionBar;
import android.app.ListActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ArrayAdapter;

import com.example.labb2da523b.db.Expense;
import com.example.labb2da523b.db.SqlHelper;

public class LatestTransactions extends ListActivity {

	SqlHelper db;
	ArrayList<Expense> expenses;
	String[] listRows;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_latest);

		ActionBar ab = getActionBar();
		ab.setDisplayShowCustomEnabled(true);
		ab.setDisplayShowTitleEnabled(false);
		ab.setCustomView(R.layout.title_theme);

		db = new SqlHelper(this);
		try {
			db.open();
			expenses = db.getAllExpenses();
			db.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		Collections.reverse(expenses);
		if(expenses.size()<5) {
			listRows = new String[expenses.size()];
		} else {
			listRows = new String[5];
		}
		for(int i = 0; i<expenses.size(); i++) {
			if(i<5) {
				listRows[i] = expenses.get(i).getName()  +"  "+  expenses.get(i).getCategory() +"  "
						+ expenses.get(i).getSum() +":-  "+ expenses.get(i).getDate();
			}
		}

		setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, listRows)); 
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		return MenuHelper.Menu(item, this);
	}
}